﻿package XFComponent
{
	import flash.display.Graphics;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	
	public class XFImage extends Sprite
	{
		private var _asset : DisplayObject	=	null;
		private var _loadingIcon : DisplayObject;
		private var _enabled : Boolean	=	true;

		public function XFImage() {
			
			_loadingIcon	=	new XFLoadingIcon;
			
			updateLoadingIcon();
		}
		
		[inspectable(defaultValue = null) ]
		public function set source( val : DisplayObject ) : void
		{
			if( val == null || _asset == val )
				return;
				
			if( _asset != null )
			{
				try
				{
					removeChild( _asset );
				}
				catch( ex : * ){}
			}
				
			addChild( val );
			
			_asset	=	val;
			
			//
			updateLoadingIcon();
		}
		
		public function drawSize( w : Number, h : Number, color : uint = 0x000088, alpha : Number = 0.5 ) : void
		{
			var g : Graphics	=	this.graphics;
			g.beginFill( color, alpha );
			g.drawRect( 0, 0, w, h );
			g.endFill();
			
			_loadingIcon.x	=	w * 0.5;
			_loadingIcon.y	=	h * 0.5;
		}
		
		public function get source() : DisplayObject
		{
			return _asset;
		}
		
		private function updateLoadingIcon() : void
		{
			// Remove loading icon
			if( _asset != null )
			{
				try
				{
					removeChild( _loadingIcon );
					_loadingIcon = null;
					
				}
				catch( ex : * ){}
			}
				
			else
				addChild( _loadingIcon );
		}
		
		public function set enabled( val : Boolean ) : void
		{
			_enabled	=	val;
			
			this.mouseChildren	=	_enabled;
			this.mouseEnabled	=	_enabled;
		}
		
		public function get enabled() : Boolean
		{
			return _enabled;
		}
	}
}
